import O1HD, { managers } from "../../../../engines/gameEngine";
import levelAvoidBase from "../levelAvoidBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class levelAvoidPlayer extends cc.Component {

    @property({ type: levelAvoidBase, displayName: "主节点" })
    levelbase: levelAvoidBase = null;
    @property()
    lifeNum: number = 3;
    @property({ type: cc.Node, displayName: "剩余生命值" })
    lifeGroup: cc.Node = null
    @property({ displayName: "是否无敌" })
    isWudi: boolean = false;

    nowLifeNum = null;
    onLoad() {
        this.nowLifeNum = this.lifeNum;

        this.setLifeGroup()
    }
    setLifeGroup(ani: boolean = false) {

        if (!this.lifeGroup) return;
        for (let i = 0; i < this.lifeGroup.childrenCount; i++) {
            this.lifeGroup.children[i].active = i <= this.nowLifeNum;
        }

        if (ani) {
            let node = this.lifeGroup.children[this.nowLifeNum];
            if (node) {
                cc.tween(node)
                    .to(0.2, { opacity: 0 })
                    .to(0.2, { opacity: 255 })
                    .to(0.2, { opacity: 0 })
                    .to(0.2, { opacity: 255 })
                    .to(0.2, { opacity: 0 })
                    .call(() => {
                        node.opacity = 255
                        node.active = false
                    })
                    .start()
            }
        } else {
            this.lifeGroup.children[this.nowLifeNum] && (this.lifeGroup.children[this.nowLifeNum].active = false);
        }

    }
    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        // console.log('on collision enter');
        if(!this.levelbase.startGame)return
        if (this.levelbase.isPause && this.levelbase.playStopMove) return;
        if (this.isWudi) return
        switch (other.tag) {
            case 444:
                this.death()
                break;
        }
    }
    isDeathIng: boolean = false;
    death() {
        if (this.isDeathIng) return
        this.isDeathIng = true;
        this.nowLifeNum--;
        this.setLifeGroup(true)
        O1HD.getManagers().soundMgr.playSound("fontChase/death").then();
        O1HD.getManagers().soundMgr.playVibrate();

        if (this.nowLifeNum == 0) {
            this.levelbase.gamePause(true)

            let nowScale = this.node.scale;
            cc.tween(this.node)
                .to(0.2, { scale: nowScale - 0.2, opacity: 255 })
                .to(0.2, { scale: nowScale, opacity: 100 })
                .to(0.2, { scale: nowScale - 0.2, opacity: 255 })
                .to(0.2, { scale: nowScale, opacity: 100 })
                .to(0.2, { scale: nowScale, opacity: 255 })
                .call(() => {
                    this.levelbase.Lose();

                })
                .start()
        } else {
            let endscale: number = 0;
            switch (this.nowLifeNum) {
                case 2: endscale = 0.75; break;
                case 1: endscale = 0.5; break;
            }

            let nowScale = this.node.scale;
            cc.tween(this.node)
                .to(0.2, { scale: endscale, opacity: 255 })
                .to(0.2, { scale: nowScale, opacity: 100 })
                .to(0.2, { scale: endscale, opacity: 255 })
                .to(0.2, { scale: nowScale, opacity: 100 })
                .to(0.2, { scale: endscale, opacity: 255 })
                .call(() => {
                    this.levelbase.playStopMove = true;
                })
                .delay(1)
                .call(() => {
                    this.isDeathIng = false;
                    // this.levelbase.gamePause(false)
                })
                .start()
        }
    }
}